package me.keiwu.user.controller.pub;

import com.alibaba.fastjson.JSON;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import me.keiwu.pub.data.ConfigType;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.config.AppConfigBean;
import me.keiwu.pub.data.service.UserInfoBean;
import me.keiwu.pub.util.CacheUtil;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.pub.util.wx.CustomUtil;
import me.keiwu.user.service.UserDeliveryAddressService;
import me.keiwu.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.Arrays;

@RestController
@RequestMapping("/pub/custom")
public class CustomPubController {

    private static final Logger logger = LoggerFactory.getLogger(CustomPubController.class);

    private static final String TOKEN = "v5tBTl4UySrRvg";

    @Autowired
    private UserService userService;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private CustomUtil customUtil;


    @ResponseBody
    @RequestMapping(value = "receive", produces="application/json; charset=utf-8")
    public String receive(HttpServletRequest req, String signature, String timestamp, String nonce, String echostr) {
        logger.info("receive signature={}, timestamp={}, nonce={}, echostr={}", signature, timestamp, nonce, echostr);

        String[] array = {TOKEN, timestamp, nonce};
        Arrays.sort(array);
        String implode = StringUtils.join(array);
        HashFunction hf = Hashing.sha1();
        HashCode hc = hf.newHasher().putString(implode, Charset.defaultCharset()).hash();
        String resStr = hc.toString();
        if( resStr.equals(signature) ){
//            String openId = req.getParameter("openid");
//            if (StringUtils.isNotBlank(openId)) {
//                UserInfoBean bean = userService.getUserInfoByOpenId(openId);
//                AppConfigBean appConfig = cacheUtil.getConfig(ConfigType.APP_CONFIG, AppConfigBean.class);
//                if (appConfig != null && StringUtils.isNotBlank(appConfig.getCustomReplyJson())) {
//                    customUtil.sendCustomLinkMsg(bean.getOpenId(), appConfig.getCustomReplyJson());
//                } else {
//                    logger.error("no app config!!");
//                }
//            }
            return echostr;
        }
        return "decode fail!";
    }


    public static void main(String[] args) {
        String[] str = {"abc", "efg", "kkk"};
        String implode = StringUtils.join(str);
        System.out.println(implode);
    }

}
